package com.ttg.web.dao.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttg.common.entities.BasePage;
import com.ttg.model.dto.RoleListDTO;
import com.ttg.model.mapper.AdminRoleMapper;
import com.ttg.model.pojo.AdminRole;
import com.ttg.model.vo.RoleVO;
import com.ttg.web.dao.AdminRoleDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author ZQ
 * @version 1.0
 * @Description
 * @date 2021/1/27 10:03
 */
@Service
public class AdminRoleDaoImpl extends ServiceImpl<AdminRoleMapper, AdminRole> implements AdminRoleDao {

    private String lang = "zh-cn";


    @Override
    public Integer getCountByRoleName(String roleName) {
        LambdaQueryWrapper<AdminRole> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AdminRole::getRoleName,roleName);
        return count(wrapper);
    }

    @Override
    public Integer getCountByRoleId(Long roleId) {
        LambdaQueryWrapper<AdminRole> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AdminRole::getRoleId,roleId);
        return count(wrapper);
    }

    @Override
    public Integer getCountByRoleIdAndRoleName(Long roleId, String roleName) {
        LambdaQueryWrapper<AdminRole> wrapper = Wrappers.lambdaQuery();
        wrapper.ne(AdminRole::getRoleId,roleId)
                .eq(Objects.nonNull(roleName),AdminRole::getRoleName,roleName);
        return count(wrapper);
    }

    @Override
    public Long saveAdminRole(AdminRole adminRole) {
        save(adminRole);
        LambdaQueryWrapper<AdminRole> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AdminRole::getRoleName,adminRole.getRoleName())
                .eq(AdminRole::getRoleAuths,adminRole.getRoleAuths());
        AdminRole one = getOne(wrapper);
        return one.getRoleId();
    }

    @Override
    public AdminRole findRoleAuths(Long roleId) {
        LambdaQueryWrapper<AdminRole> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AdminRole::getRoleId,roleId);
        return getOne(wrapper);
    }

    @Override
    public boolean updateAdmRole(AdminRole adminRole, Long roleId) {
        LambdaQueryWrapper<AdminRole> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AdminRole::getRoleId,roleId);
        return update(adminRole,wrapper);
    }

    @Override
    public boolean deleteByRoleId(Long roleId) {
        LambdaQueryWrapper<AdminRole> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AdminRole::getRoleId,roleId);
        return remove(wrapper);
    }

    @Override
    public BasePage<AdminRole> selectListByKeywordAndRoleTypeAndOrgNo(RoleListDTO reqDto) {
        LambdaQueryWrapper<AdminRole> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotBlank(reqDto.getRoleName()),AdminRole::getRoleName,reqDto.getRoleName())
                .or()
                .like(StringUtils.isNotBlank(reqDto.getRoleName()),AdminRole::getRoleId,reqDto.getRoleName())
                .orderByDesc(AdminRole::getRoleOrgNo).orderByDesc(AdminRole::getRoleId);
        BasePage<AdminRole> page = reqDto.parse();
        return page(page,wrapper);
    }


    @Override
    public AdminRole selectOneByIdAndOrgNO(Long roleId) {
        LambdaQueryWrapper<AdminRole> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AdminRole::getRoleId,roleId);
        return getOne(wrapper);
    }

    @Override
    public List<RoleVO> role(String lang, List<Long> roleHideId) {
        return baseMapper.role(lang,roleHideId);
    }

    @Override
    public AdminRole findRoleById(Long roleId) {
        LambdaQueryWrapper<AdminRole> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AdminRole::getRoleId,roleId);
        return getOne(wrapper);
    }


    @Override
    public List<AdminRole> selectListByRoleIds(List<Long> roleId) {
        LambdaQueryWrapper<AdminRole> wrapper = Wrappers.lambdaQuery();
        wrapper.in(AdminRole::getRoleId,roleId);
        return list(wrapper);
    }



}
